package com.lbssoft.gpsbd.dao;

import com.lbssoft.gpsbd.dao.sql.DeviceSql;
import com.lbssoft.gpsbd.dao.sql.UserSql;
import com.lbssoft.gpsbd.model.vo.LoginParam;
import com.lbssoft.gpsbd.model.vo.UserParam;
import com.lbssoft.gpsbd.model.vo.UserVo;
import org.apache.ibatis.annotations.*;

import com.lbssoft.gpsbd.model.User;
import org.omg.CORBA.Object;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {
	/**
	 * 注册用户
	 * @param user
	 */
	@Insert("INSERT into lbs_users(account,email,phone,hashedPassword,salt) VALUES(#{account},#{email},#{phone},#{hashedPassword},#{salt})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void addUser(User user);
	
	/**
	 * 刷新用户token
	 */
	@Update("UPDATE lbs_users SET token= #{token} where id = #{id}")
	void udpateToken(@Param("token") String token,@Param("id") long id);
	
	/**
	 * 根据token获取用户信息
	 * @param token
	 * @return
	 */
	@Select("SELECT t.*,GROUP_CONCAT(a.roleId) as roleIds,GROUP_CONCAT(b.roleKey) as roleKeys,max(b.groupId) as groupId FROM lbs_users t LEFT JOIN lbs_user_role a on a.userId = t.id LEFT JOIN lbs_roles b on b.id = a.roleId where t.token = #{token} GROUP BY id")
	UserVo getUserByToken(@Param("token") String token);

	/**
	 * 根据用户名获取帐号信息
	 * @param account
	 * @return
     */
	@Select("SELECT * FROM lbs_users WHERE account = #{account}")
	User getUserByAccount(@Param("account") String account);

	/**
	 * 根据id获取用户信息
	 * @param id
	 * @return
     */
	@Select("SELECT id,account,token FROM lbs_users WHERE id = #{id}")
	Map<String,Object> getUserById(@Param("id") long id);

	/**
	 * 修改密码
	 * @param
     */
	@Update("UPDATE lbs_users SET hashedPassword=#{hashedPassword},salt=#{salt} WHERE id= #{id}")
	void updatePassword(@Param("id") long id,@Param("hashedPassword") String hashedPassword,@Param("salt") String salt);

	/**
	 * 获取用户列表
	 * @return
     */
	@Select("SELECT t.id,t.account,t.email,t.phone,t.disabled,t.status,t.time,GROUP_CONCAT(roleId) as roleIds,GROUP_CONCAT(roleKey) as roleKeys FROM lbs_users t LEFT JOIN lbs_user_role a ON a.userId = t.id LEFT JOIN lbs_roles b on b.id = a.roleId ${where} GROUP BY t.id order by t.id desc")
	List<UserVo> getUsers(@Param("where") String where);

	/**
	 * 删除用户
	 * @param id
     */
	@Delete("DELETE FROM lbs_users WHERE id = #{id}")
	void delete(@Param("id") long id);

	/**
	 * 修改用户的角色
	 * @param roleId
	 * @param userId
     */
	@Update("UPDATE lbs_user_role SET roleId = #{roleId} WHERE userId = #{userId}")
	void updateRole(@Param("roleId") int roleId,@Param("userId") long userId);

	/**
	 * @param roleId
	 * @param userId
     */
	@Insert("INSERT INTO lbs_user_role(userId,roleId) VALUE(#{userId},#{roleId})")
	void addUserRole(@Param("roleId") int roleId,@Param("userId") long userId);

	/**
	 * 修改用户信息
	 * @param user
     */
	//@Select("UPDATE lbs_users SET email=#{email},phone=#{phone},disabled= #{disabled} WHERE id = #{id}")
	@UpdateProvider(type = UserSql.class, method = "update")
	void update(UserParam user);

	/**
	 * 删除用户角色
	 * @param userId
     */
	@Delete("DELETE FROM lbs_user_role where userId = #{userId}")
	void deleteUserRole(@Param("userId") long userId);

	/**
	 * 清理用户token
	 * @param id
     */
	@Update("UPDATE lbs_users SET token =#{token} where id = #{id}")
	void cleanToken(@Param("token") String token,@Param("id") long id);

	/**
	 * 更新用户在线状态
	 * @param status
	 * @param id
     */
	@Update("UPDATE lbs_users SET `status`=#{status} where id = #{id}")
	void updateStatus(@Param("status") int status,@Param("id") long id);


}
